package ar.edu.itba.it.paw.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import ar.edu.itba.it.paw.dao.UserDAO;
import ar.edu.itba.it.paw.domain.ConnectionManager;
import ar.edu.itba.it.paw.domain.User;
import ar.edu.itba.it.paw.domain.exceptions.DatabaseException;


public class DatabaseUserDAO implements UserDAO{

	private static UserDAO instance;
	
	public static synchronized UserDAO getInstance(){
		if(instance == null)
			instance = new DatabaseUserDAO();
		return instance;
	}
	
	@Override
	public User getUser(String username) {
		ConnectionManager manager = ConnectionManager.getInstance();
		Connection conn = manager.getConnection();
		PreparedStatement stmt;
		User user = null;
		
		try{
			stmt = conn.prepareStatement("select * from \"Usuario\" where \"userName\" = ?");
			stmt.setString(1, username);
			ResultSet cur = stmt.executeQuery();
			if(cur.next()){
			user = new User(cur.getString("userName"),cur.getString("password"), cur.getString("nombre"), cur.getString("apellido"), cur.getString("mail"));
			user.setId(cur.getInt("userId"));
			user.setAdmin(cur.getBoolean("admin"));
			} else {
				manager.closeConnection();
				return null;
			}
		}catch(Exception e){
			throw new DatabaseException();
		}
		manager.closeConnection();
	
		return user;
		
	}
	
	@Override
	public boolean existsMail(String mail) {
		ConnectionManager manager = ConnectionManager.getInstance();
		Connection conn = manager.getConnection();
		PreparedStatement stmt;
		
		try{
			stmt = conn.prepareStatement("select * from \"Usuario\" where mail = ?");
			stmt.setString(1, mail);
			ResultSet cur = stmt.executeQuery();
			if(cur.next()){
				manager.closeConnection();
				return true;
			}
		}catch(Exception e){
			throw new DatabaseException();
		}
		manager.closeConnection();
		return false;
	}
	
	
	public boolean existsUser(User user){
		if(this.getUser(user.getUsername()) == null && this.existsMail(user.getMail()) == false){
			return false;
		}
		return true;
	}
	
	
	public User getUser(int id){
		ConnectionManager manager = ConnectionManager.getInstance();
		Connection conn = manager.getConnection();
		PreparedStatement stmt;
		User user = null;
		
		try{
			stmt = conn.prepareStatement("select * from \"Usuario\" where \"userId\" = ?");
			stmt.setInt(1,id);
			ResultSet cur = stmt.executeQuery();
			if(cur.next()){
				user = new User(cur.getString("userName"),cur.getString("password"), cur.getString("nombre"), cur.getString("apellido"), cur.getString("mail"));
				user.setId(cur.getInt("userId"));
				user.setAdmin(cur.getBoolean("admin"));
			} else {
				manager.closeConnection();
				return null;
			}
		}catch(Exception e){
			throw new DatabaseException();
		}
		manager.closeConnection();
	
		return user;
	}

	@Override
	public void addUser(User user) {
		ConnectionManager manager = ConnectionManager.getInstance();
		Connection conn = manager.getConnection();
		PreparedStatement stm;
		try {
			stm = conn.prepareStatement("INSERT INTO \"Usuario\" (\"userName\", nombre, apellido, \"password\", mail) VALUES (?, ?, ?, ?, ?)"); 
			stm.setString(1, user.getUsername());		
			stm.setString(2, user.getNombre());
			stm.setString(3, user.getApellido());
			stm.setString(4, user.getPassword());
			stm.setString(5, user.getMail());
			stm.execute();
		} catch (SQLException e) {
			throw new DatabaseException();
		}
		manager.closeConnection();
	}
}
